package com.dingwei.superiordiary.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.easyViewHolder.LogisticsHolder;
import com.dingwei.superiordiary.base.LogisticsBean;
import com.dingwei.superiordiary.mvp.presenter.LogisticsPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.weight.TitleBar;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/2/27.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class LogisticsActivity extends BaseActivity implements Contract.LogisticsView {
    LogisticsPresenter logisticsPresenter = new LogisticsPresenter(this);
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.mRecycleView)
    RecyclerView mRecycleView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_logistics_list;
    }

    RecyclerArrayAdapter adapter = new RecyclerArrayAdapter(BaseActivity.context) {
        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new LogisticsHolder(parent);
        }
    };

    @Override
    protected void initData() {
        logisticsPresenter.initData();
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleView.setLayoutManager(layoutManager);
        mRecycleView.setNestedScrollingEnabled(false);
        mRecycleView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.parting_line), 1));

        mRecycleView.setAdapter(adapter);


    }

    @Override
    protected void initListener() {
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                LogisticsBean.DataBean data = (LogisticsBean.DataBean) adapter.getItem(position);
                intent = new Intent();
                intent.putExtra("Logistics_info", data);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void getData(LogisticsBean bean) {
        showData("success");
        adapter.clear();
        adapter.addAll(bean.data);
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    public void showData(String state) {
        if (state.equals("success")) {
            mRecycleView.setVisibility(View.VISIBLE);
            noDataLayout.setVisibility(View.GONE);
        } else {
            mRecycleView.setVisibility(View.GONE);
            noDataLayout.setVisibility(View.VISIBLE);
        }
    }
}
